package com.to8to.kitt.esb.http;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.codec.http.HttpContentCompressor;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

public class HttpServerInitializer extends ChannelInitializer<Channel> {
	
	ChannelHandler bizhandler;
	
	public HttpServerInitializer (ChannelHandler bizhandler)
	{
		this.bizhandler = bizhandler;
	}
	
	
    @Override
    public void initChannel(Channel ch) throws Exception {
        ChannelPipeline p = ch.pipeline();

        // Uncomment the following line if you want HTTPS
        //SSLEngine engine = SecureChatSslContextFactory.getServerContext().createSSLEngine();
        //engine.setUseClientMode(false);
        //p.addLast("ssl", new SslHandler(engine));

        p.addLast(new LoggingHandler(LogLevel.TRACE));
        
        p.addLast("codec", new HttpServerCodec());
        
        p.addLast("aggegator", new HttpObjectAggregator(512 * 1024 * 1024));
        
        p.addLast("deflater", new HttpContentCompressor(1));
        
        p.addLast("handler", bizhandler);
    }
}
